package com.lykj.llw.box.ui.activity.agent

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.dinuscxj.itemdecoration.LinearDividerItemDecoration
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.ChooseGameAdapter
import com.lykj.llw.box.adapter.ChooseServerAdapter
import com.lykj.llw.box.data.bean.response.ChooseGameResponse
import com.lykj.llw.box.databinding.ActAgentGiftCodeBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 代理-礼包码页面
 * ================================================
 *
 * @author gosha
 * @date 2023/10/30-18:01
 */
class AgentGiftCodeActivity : BaseActivity() {
    lateinit var binding: ActAgentGiftCodeBinding

    private var mTextWatcher: TextWatcher? = null
    private val mAdapter = ChooseGameAdapter()
    private val mData = ArrayList<ChooseGameResponse.DataDTO>()
    private var mSelectGameId = -1

    private var mIsShow = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActAgentGiftCodeBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "礼包码"
        binding.titleBar.tvRightOption.visibility = View.GONE

        initRCV()
    }

    private fun initRCV() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvChoose.layoutManager = linearLayoutManager
        binding.rcvChoose.setHasFixedSize(true)

        val dividerItemDecoration = LinearDividerItemDecoration(
            this, LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL
        )
        dividerItemDecoration.setDivider(
            resources.getDrawable(
                R.drawable.divider_5dp, resources.newTheme()
            )
        )
        binding.rcvChoose.addItemDecoration(dividerItemDecoration, 0)
        mAdapter.data = mData
        mAdapter.setOnItemClickListener { adapter, view, position ->
            mSelectGameId = mData[position].id
            //隐藏列表显示礼包
            binding.etChooseGame.setText("${mData[position].name}")
            binding.lltGiftBag.visibility = View.VISIBLE
            binding.rcvChoose.visibility = View.GONE
            binding.ivChoose.setImageResource(R.mipmap.ic_pull_down)
        }
        binding.rcvChoose.adapter = mAdapter
    }

    //获取下拉游戏
    private fun obtainChooseGame(name: String) {
        RetrofitManager.instance.obtainChooseGame(object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@AgentGiftCodeActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                val bean = GsonUtils.fromJson(
                    responseBody, ChooseGameResponse::class.java
                )

                mData.clear()
                if (bean.data.size > 0) {
                    mData.addAll(bean.data)
                } else {
                    binding.rcvChoose.visibility = View.GONE
                    ToastUtils.showShort(this@AgentGiftCodeActivity, "没有搜索到该游戏信息")
                }

                mAdapter.notifyDataSetChanged()
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let {
                    ToastUtils.showShort(this@AgentGiftCodeActivity, msg)
                }
            }
        }, name)
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.lltChooseGame.clickWithTrigger(50) {
            mIsShow = !mIsShow
            if (mIsShow) {
                binding.ivChoose.setImageResource(R.mipmap.ic_pull_up)
                if (mData.isEmpty()) {
                    obtainChooseGame(binding.etChooseGame.text.toString())
                }
                binding.rcvChoose.visibility = View.VISIBLE
            } else {
                binding.ivChoose.setImageResource(R.mipmap.ic_pull_down)
                binding.rcvChoose.visibility = View.GONE
            }
        }

        mTextWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                if (s.toString().isNotEmpty()) {
                    obtainChooseGame(binding.etChooseGame.text.toString())
                }
            }
        }
        binding.etChooseGame.addTextChangedListener(mTextWatcher)

        KeyboardUtils.registerSoftInputChangedListener(this) {
            if (it == 0) {
                binding.etChooseGame.clearFocus()
            }
        }
    }

    override fun onDestroy() {
        binding.etChooseGame.removeTextChangedListener(mTextWatcher)
        removeDisposables(this)
        super.onDestroy()
    }
}